package com.likianta.anykey;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcool.circletextimageview.CircleTextImageView;

import java.util.List;

/**
 * Created by Likianta_DoDoRa on 2017/11/21 0021.
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
    
    private List<Card> cardList;
    private Context context;
    
    public CardAdapter(List<Card> cardList) {
        this.cardList = cardList;
    }
    
    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        
        View card = LayoutInflater.from(context).inflate(R.layout.card_type1, parent, false);
        // inflate的第三个参数表示是否连接该布局和父控件。由于系统已经插入了这个布局到父控件，所以设为false（若写true则会产生一个多余的parent）
        
        return new CardViewHolder(card); // 获得了一张白卡
    }
    
    // 开始给白卡绑数据
    @Override
    public void onBindViewHolder(CardViewHolder whiteCard, int position) {
        Card card = cardList.get(position); // 通过位置参数判断用户点的是哪个卡片
        
        whiteCard.cardTitle.setText(card.getCardTitle());
        whiteCard.cardSummary.setText(card.getCardSummary());
        
        if (card.getCardHeadResId() == 0) {
            whiteCard.cardHead.setText(card.getCardHeadText());
            whiteCard.cardHead.setTextSize(60);
            whiteCard.cardHead.setTextColor(card.getCardHeadTextColor());
            whiteCard.cardHead.setFillColor(card.getCardHeadFillColor());
        } else {
            whiteCard.cardHead.setImageResource(card.getCardHeadResId());
        }
        
    }
    
    // 获取列表中卡片总数量
    @Override
    public int getItemCount() {
        return cardList.size();
    }
    
    public void add(int position, Card card) {
        cardList.add(position, card);
        notifyItemInserted(position);
    }
    
    public void remove(Context context, int position) {
        if (position < 0) {
            Toast.makeText(context, "列表中并没有结果!", Toast.LENGTH_SHORT).show();
        } else {
            cardList.remove(position);
            notifyItemRemoved(position);
        }
    }
    
    static class CardViewHolder extends RecyclerView.ViewHolder {
        
        CardView cardView;
        TextView cardTitle;
        TextView cardSummary;
        CircleTextImageView cardHead;
        
        public CardViewHolder(View view) {
            //引入外界的item，在本类中开始实例化
            super(view);
            
            cardView = (CardView) view;
            cardTitle = (TextView) view.findViewById(R.id.card_title);
            cardSummary = (TextView) view.findViewById(R.id.card_sum);
            cardHead = (CircleTextImageView) view.findViewById(R.id.card_head);
        }
    }
    
}
